package com.fand.common.util;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fand.common.annotation.DataField;
import com.fand.common.enums.ConditionOnField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * QueryWrapperUtil查询条件工具类
 * <p>
 * Author: zhangjiabin
 * Email: javaboyok@163.com
 * CreateDate: 2021/6/6
 * Version: 1.0
 */
@Data
@Builder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class QueryWrapperUtil {

    private QueryWrapper queryWrapper;

    private Object object;

    public QueryWrapper builderQueryParams() {
        if (Objects.isNull(queryWrapper) || Objects.isNull(object)) {
            throw new IllegalArgumentException("参数不能为空");
        }
        Field[] declaredFields = object.getClass().getDeclaredFields();
        Arrays.stream(declaredFields).forEach(field -> {
                    try {
                        field.setAccessible(true);
                        DataField dataField = field.getDeclaredAnnotation(DataField.class);
                        if (Objects.isNull(dataField)) {
                            return;
                        }
                        String tableField = dataField.tableFiled();
                        ConditionOnField condition = dataField.condition();
                        Object value = field.get(object);
                        if (Objects.nonNull(value)) {
                            switch (condition) {
                                case EQ:
                                    queryWrapper.eq(tableField, value);
                                    break;
                                case GT:
                                    queryWrapper.gt(tableField, value);
                                    break;
                                case GE:
                                    queryWrapper.ge(tableField, value);
                                    break;
                                case LT:
                                    queryWrapper.lt(tableField, value);
                                    break;
                                case LE:
                                    queryWrapper.le(tableField, value);
                                    break;
                                case LIKE_RIGHT:
                                    queryWrapper.likeLeft(tableField, value);
                                    break;
                                case LIKE_LEFT:
                                    queryWrapper.likeRight(tableField, value);
                                    break;
                            }
                        }
                    } catch (IllegalAccessException e) {
                        log.error("com.fand.common.util.QueryWrapperUtil builderQueryParams error", e);
                    }
                }
        );
        return queryWrapper;
    }


}
